<template>
  <el-dialog :title="formData.id?'编辑':'添加权限'" :visible="showDialog" @close="btnCancel">
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="开启">
        <!-- '1' 表示开启   '0' 表示关闭-->
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="submit">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addQuanXian, getQuanXianById, editQuanXian } from '@/api/quanxian'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    alltableDatalist: {
      type: Array,
      default: _ => []
    }

  },
  data() {
    const checkName = (rule, val, callback) => {
      let arrone
      if (this.formData.id) {
        arrone = this.alltableDatalist.filter(item => item.id !== this.formData.id && item.pid === '0')
      } else {
        arrone = this.alltableDatalist.filter(item => item.pid === '0') // 一级权限
      }
      const flag = arrone.every(item => item.title !== val)
      flag ? callback() : callback(`${val} - 权限名称已经存在`)
    }
    const checkCode = (rule, val, callback) => {
      let flag
      if (this.formData.id) {
        flag = this.alltableDatalist.filter(item => item.id !== this.formData.id).every(item => item.code !== val)
      } else {
        flag = this.alltableDatalist.every(item => item.code !== val)
      }
      flag ? callback() : callback(`${val} - 权限标识已经存在`)
    }
    return {
      formData: {
        title: '', // 标题
        code: '', // 标识
        description: '', // 描述
        pid: '0', // 父权限 id 没有的话就是 0
        type: '1' // 层级类型 1 或者 2
      },
      // 表单校验规则
      rules: {
        title: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取id对应的数据
    async getQuanxian(id) {
      const res = await getQuanXianById(id)
      this.formData = res.data
    },
    // 确定
    async submit() {
      console.log(this.alltableDatalist)
      this.$refs.form.validate() // 整体表单校验
      if (this.formData.id) {
        await editQuanXian(this.formData) // 编辑权限
      } else {
        await addQuanXian(this.formData) // 新增权限
      }
      this.btnCancel() // 关闭弹窗
      this.$emit('updateEvent') // 获取最新数据，刷新页面
    },
    // 点击取消，清空表单，清空校验规则,关闭弹框
    btnCancel() {
      this.formData = {
        title: '', // 标题
        code: '', // 标识
        description: '', // 描述
        pid: '0', // 父权限 id 没有的话就是 0
        type: '1' // 层级类型 1 或者 2
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
